package com.suoluo.zenadmin.service.classify;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.dao.classify.StyleDAO;
import com.suoluo.zenadmin.dao.home.HomeDAO;
import com.suoluo.zenadmin.oss.OSSService;
import com.suoluo.zenadmin.oss.OSSService.OSSImageException;
import com.suoluo.zenadmin.service.ServiceException;
import com.suoluo.zenadmin.service.dest.DestFeatureCategoryService;
import com.suoluo.zenadmin.vo.PageVO;
import com.suoluo.zenadmin.vo.classify.HomeStyleVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureCategoryVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;


public class StyleServiceImpl implements StyleService {

	@Autowired
	private StyleDAO styleDAO;
	@Autowired
	private HomeDAO homeDAO;
	@Resource(name="ossService")
	private OSSService ossService;
	
	@Resource(name="categoryService")
	private DestFeatureCategoryService categoryService;
	

	@Override
	public List<HomeStyleVO> findListStyleHomeItem(PageVO page) {
		int curr = page.getPage();
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(curr, rows);
		return styleDAO.findListStyleHomeItem(start,rows);
	}

	@Override
	public void saveStyle(HomeStyleVO styleVO) {
		int featureCategoryId = styleVO.getFeatureCategoryId();
		
		int res = styleDAO.findStyleHomeCategoryByCategoryId(styleVO
				.getFeatureCategoryId());
		if (res > 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "该类别信息已存在，不可重复添加！");
		}
		String imgName = null;
		try {
			DestFeatureCategoryVO featureCategory = categoryService.findDestFeatureCategoryById(featureCategoryId);
			styleVO.setFeatureCategoryName(featureCategory.getFeatureCategoryName());
			int count = styleDAO.findFeatureCategoryCount(featureCategoryId);
			styleVO.setStyleCount(count);
			imgName = ossService.putImage(styleVO.getMfile());
			styleVO.setStyleOrigImg(imgName);
			styleDAO.saveStyle(styleVO);
		}catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "风格偏好首页类别图片保存失败！",e);
		} catch (Exception e) {
			ossService.deleteImage(imgName);
			throw new ServiceException(CommonResp.FAIL_CODE, "风格偏好首页信息保存失败！",e);
		}
	}

	@Override
	public HomeStyleVO findHomeStyleItemById(int styleId) {
		return styleDAO.findHomeStyleItemById(styleId);
	}

	@Override
	public void updateStyle(HomeStyleVO styleVO) {
		String imgName = null;
		try {
			MultipartFile mfile = styleVO.getMfile();
			if(mfile != null && !mfile.isEmpty())
			{
				imgName = ossService.updateImage(mfile, styleVO.getStyleOrigImg());
				styleVO.setStyleOrigImg(imgName);
			}
			styleDAO.updateStyle(styleVO);
		} catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "风格偏好首页类别图片更新失败！",e);
		}catch (Exception e) {
			ossService.deleteImage(imgName);
			throw new ServiceException(CommonResp.FAIL_CODE, "风格偏好首页信息更新失败！",e);
		}
	}

	@Override
	public void deleteStyle(HomeStyleVO styleVO) {
		// 删除图片
		ossService.deleteImage(styleVO.getStyleOrigImg());
		// 删除数据
		styleDAO.deleteStyle(styleVO.getHomeStyleId());
	}

	@Override
	public void updateStyleCount(int homeStyleId, int categoryId) {
		// 查询类别项总数
		int count = styleDAO.findFeatureCategoryCount(categoryId);
		// 更新数据
		styleDAO.updateStyleCount(homeStyleId, count);
	}
//
//	@Override
//	public List<DestFeatureCategoryPOJO> findListParentCategory() {
//		int hotelCategoryId = SystemConfigHolder
//				.getDestFeatureHotelCategoryId();
//		return styleDAO.findListParentCategory(hotelCategoryId);
//	}

	@Override
	public int findHomeStyleCount() {
		return styleDAO.findHomeStyleCount();
	}
	
//	/**
//	 * 查询推荐的最新特色项信息
//	 * 
//	 * @param curPage
//	 * @return
//	 */
//	@Override
//	@SuppressWarnings("rawtypes")
//	public List<StyleLatestVO> findAllHomeData(int curPage, int pageRows) {
//			int length = SystemConfigHolder.getAdminContentLimit();
//
//			int startPos = PageUtil.calcStartRow(curPage, pageRows);
//			List<Map> result = styleDAO.findAllHomeData(startPos,
//					pageRows,length);
//			List<StyleLatestVO> itemList = new ArrayList<StyleLatestVO>();
//
//			List<BaseInfoVO> baseInfos = new ArrayList<BaseInfoVO>();
//			Map<String,StyleLatestVO> itemMap = new HashMap<String, StyleLatestVO>();
//			for (Map row : result) {
//				StyleLatestVO itemVO = new StyleLatestVO();
//
//				Integer featureCategoryId = (Integer) row
//						.get("t_feature_category_id");
//				itemVO.setFeatureCategoryId(featureCategoryId);
//				itemVO.setDestId((Integer) row.get("t_dest_id"));
//				itemVO.setLinkId((Integer) row.get("t_link_id"));
////				String title = (String) row.get("t_feature_item_title");
//				String zhname = (String) row.get("zhname");
//				String enname = (String) row.get("enname");
////				String desc = (String) row.get("itemDesc");
////				if (StringUtils.hasLength(desc)) {
////					itemVO.setDesc(UBB.decode(desc));
////				}
//				itemVO.setTitle(StringUtil.buildCompleteName(zhname, enname));
//				String source = (String) row.get("source");
//				String small = (String) row.get("small");
//				String normal = (String) row.get("normal");
//				itemVO.setOrigImg(source+normal);
//				itemVO.setMiniImg(source+small);
//				itemVO.setItemCreateTime((Date) row.get("t_item_create_time"));
//				itemVO.setFeatureCategoryName((String) row
//						.get("t_category_name"));
//				itemVO.setCountryName((String) row.get("country_name"));
////				itemVO.setCountryOrigFlag((String) row.get("t_country_orig_flag"));
//				itemVO.setDestName((String) row.get("destName"));
//				itemList.add(itemVO);
//				BaseInfoVO baseInfo = new BaseInfoVO();
//				baseInfo.setDataId(itemVO.getLinkId());
//				if (itemVO.getFeatureCategoryId() == SystemType.HOME_NEWS){
//					baseInfo.setInfoType(SystemType.INFO_NEWS);
//				} else if (itemVO.getFeatureCategoryId() == SystemType.HOME_DEST){
//					baseInfo.setInfoType(SystemType.INFO_DEST);
//				} else if (itemVO.getFeatureCategoryId() == SystemType.HOME_HOTEL){
//					baseInfo.setInfoType(SystemType.INFO_HOTEL);
//				} else {
//					baseInfo.setInfoType(SystemType.INFO_DEST_FEATURE_ITEM);
//				}
//				itemMap.put(baseInfo.getDataId()+"_" + baseInfo.getInfoType(), itemVO);
//				baseInfos.add(baseInfo);
//			}
//
//			List<BaseInfoVO> results = infoService.findDefaultBaseInfos(baseInfos);
//			for (BaseInfoVO info : results){
//				String key = info.getDataId()+"_" + info.getInfoType();
//				StyleLatestVO item = itemMap.get(key);
//				String desc = UBB.clear(info.getContent());
//				item.setDesc(desc);
//			}
//			return itemList;
//	}

//	@Override
//	public int findStyleLatestCount() {
//		return styleDAO.findStyleLatestCount();
//	}

//	@SuppressWarnings("rawtypes")
//	@Override
//	public List<DestVO> findAllDestList(int curPage, int pageRows) {
//		
////			int length = SystemConfigHolder.getAdminContentLimit();
//
//			int startPos = PageUtil.calcStartRow(curPage, pageRows);
////			List<StyleDestVO> list = new ArrayList<StyleDestVO>();
//			
//			List<DestVO> listRes = styleDAO.findAllDestList(startPos, pageRows, SystemType.INFO_DEST);
//			for (DestVO dest : listRes) {
////				StyleDestVO styleDestVO = new StyleDestVO();
////				styleDestVO.setDestId((Integer) map.get("t_dest_id"));
////				styleDestVO.setDestName((String) map.get("t_dest_name"));
////				styleDestVO.setDestDesc(UBB.decode((String) map
////						.get("destDesc")));
////				styleDestVO.setDestOrigImg((String) map
////						.get("t_dest_orig_img"));
////				styleDestVO.setDestMiniImg((String) map.get("t_dest_mini_img"));
////				styleDestVO.setCountryName((String) map
////						.get("t_country_name_zh"));
////				list.add(styleDestVO);
//				dest.setContent(UBB.clear(dest.getContent()));
//				dest.setName(StringUtil.buildCompleteName(dest.getZhname(), dest.getEnname()));
//			}
//			return listRes;
//	}
//
//	@Override
//	public int findDestCount() {
//		return styleDAO.findDestCount();
//	}

//	@SuppressWarnings("rawtypes")
//	@Override
//	public List<Hotel2VO> findAllHotelList(int curPage, int pageRows) {
////			int length = SystemConfigHolder.getAdminContentLimit();
//			int startPos = PageUtil.calcStartRow(curPage, pageRows);
////			List<StyleHotelVO> list = new ArrayList<StyleHotelVO>();
//			List<Hotel2VO> listRes = styleDAO.findHotels(startPos, pageRows,SystemType.INFO_HOTEL);
//			for (Hotel2VO hotel : listRes) {
//				hotel.setName(StringUtil.buildCompleteName(hotel.getZhname(), hotel.getEnname()));
//				hotel.setContent(UBB.clear(hotel.getContent()));
////				StyleHotelVO styleHotelVO = new StyleHotelVO();
////
////				styleHotelVO.setHotelId((Integer) map.get("t_hotel_id"));
////				styleHotelVO.setHotelName((String) map.get("t_hotel_name"));
////				String desc = UBB.clear((String) map.get("hotelDesc"));
////				if (desc.length() > length) {
////					styleHotelVO.setHotelDesc(desc.substring(0, length));
////				} else {
////					styleHotelVO.setHotelDesc(desc);
////				}
////				styleHotelVO.setHotelOrigImg((String) map
////						.get("t_hotel_orig_img"));
////				styleHotelVO.setHotelMiniImg((String) map
////						.get("t_hotel_mini_img"));
////				styleHotelVO
////						.setDestName((String) map.get("t_dest_name_zh"));
////				if (map.get("reviewCount") != null) {
////					BigDecimal dec = (BigDecimal) map.get("reviewCount");
////					int reviewCount = dec.intValue();
////					styleHotelVO.setReviewCount(reviewCount);
////				}
////				if (map.get("t_hotel_score") != null) {
////					styleHotelVO.setHotelScore((Double) map
////							.get("t_hotel_score"));
////				}
////
////				if (map.get("t_hotel_price") != null) {
////					styleHotelVO.setHotelPrice((String) map
////							.get("t_hotel_price"));
////				}
////				if (map.get("t_hotel_houses") != null) {
////					styleHotelVO.setHotelHouses((String) map
////							.get("t_hotel_houses"));
////				}
////				styleHotelVO.setCountryName((String) map
////						.get("t_country_name_zh"));
////
////				styleHotelVO.setHotelEmail((String) map.get("t_hotel_email"));
////				styleHotelVO.setHotelUrl((String) map.get("t_hotel_url"));
////				styleHotelVO.setHotelLogoImg((String) map.get("t_hotel_logo_img"));
////
////				list.add(styleHotelVO);
//			}
//			return listRes;
//	}
//
//	@Override
//	public int findHotelCount() {
//		return styleDAO.findHotelCount();
//	}

	@Override
	public List<DestFeatureItem2VO> findFeatureItemByCategoryId(
			int featureCategoryId, int currPage, int pageRows) {
		int startPos = PageUtil.calcStartRow(currPage, pageRows);
		List<DestFeatureItem2VO> list = styleDAO.findFeatureItemByCategoryId(featureCategoryId,startPos,pageRows);
//		List<FeatureItemVO> resList = new ArrayList<FeatureItemVO>();
//		int length = SystemConfigHolder.getAdminContentLimit();
		for (DestFeatureItem2VO item : list) {
			item.setName(StringUtil.buildCompleteName(item.getZhname(), item.getEnname()));
		}
		return list;
	}

	@Override
	public int findFeatureItemCountByCategoryId(int featureCategoryId) {
		return styleDAO.findFeatureItemCountByCategoryId(featureCategoryId);
	}
}
